package hongniang

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"time"
)

type RegisterBody struct {
	Phone string `json:"phone" gorm:"phone"`
	Pwd   string `json:"pwd" gorm:"pwd"`
}

func Login(ctx *gin.Context) {
	request := RegisterBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	pwd := request.Pwd

	var user models.Hongniang
	data_source.Db.Where("phone = ?", phone).First(&user)
	//reqip := ctx.ClientIP()
	if user.ID == 0 {
		useradd := models.Hongniang{
			Phone:   phone,
			Addtime: time.Now().Unix(),
			Pwd:     pwd,
		}
		data_source.Db.Create(&useradd)
		useradd.Phone = phone
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    useradd,
			"msg":     "ok",
		})
		return

	} else {
		if user.Pwd != pwd {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "密码错误",
			})
			return
		}

		user.Phone = phone
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    user,
			"msg":     "ok",
		})
		return

	}

}

func Updateyz(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	id := request.Hongniangid
	invitecode := request.Invitecode

	if invitecode == "1001" {
		hn := models.Hongniang{
			Invitecode: invitecode,
		}
		data_source.Db.Where("id = ?", id).Updates(&hn)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    "ok",
			"msg":     "ok",
		})
		return
	}

	var hongniang models.Hongniang
	data_source.Db.Where("id = ?", id).First(&hongniang)
	if hongniang.Invitecode != "" {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请勿重复绑定",
		})
		return
	}
	var invite models.Invitecode
	data_source.Db.Where("code = ?", invitecode).First(&invite)
	if invite.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "邀请码错误",
		})
		return
	}
	if invite.Endtime < time.Now().Unix() {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "邀请码已过期",
		})
		return
	}
	if invite.Status == 2 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "邀请码已使用",
		})
		return
	}
	hn := models.Hongniang{
		Invitecode: invitecode,
	}
	data_source.Db.Where("id = ?", id).Updates(&hn)

	yqm := models.Invitecode{
		Hongniang:   hongniang.Name,
		Hongniangid: id,
		Status:      2,
	}
	data_source.Db.Where("code = ?", invitecode).Updates(&yqm)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Updatezl(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	id := request.Hongniangid
	name := request.Name
	image := request.Image
	sex := request.Sex
	shengri := request.Shengri
	cityid := request.Cityid
	city := request.City
	hn := models.Hongniang{
		Name:    name,
		Image:   image,
		Sex:     sex,
		Shengri: shengri,
		City:    city,
		Cityid:  cityid,
	}
	data_source.Db.Where("id = ?", id).Updates(&hn)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type UpdatezlBody struct {
	Hongniangid int64   `json:"hongniangid" gorm:"hongniangid"`
	Uid         int64   `json:"uid" gorm:"uid"`
	Name        string  `json:"name" gorm:"name"`
	Phone       string  `json:"phone" gorm:"phone"`
	Invitecode  string  `json:"invitecode" gorm:"invitecode"`
	Image       string  `json:"image" gorm:"image"`
	Sex         int64   `json:"sex" gorm:"sex"` // 1男 2女
	Shengri     string  `json:"shengri" gorm:"shengri"`
	Cityid      int64   `json:"cityid" gorm:"cityid"`
	City        string  `json:"city" gorm:"city"`
	Price       float64 `json:"price" gorm:"price"`
	Addtime     int64   `json:"addtime" gorm:"addtime"`
	Orderallnum int64   `json:"orderallnum" gorm:"orderallnum"` // 总接单
	Orderwcnum  int64   `json:"orderwcnum" gorm:"orderwcnum"`   // 完成订单
	Status      int64   `json:"status" gorm:"status"`           // 1正常 2禁用
	Xingming    string  `json:"xingming" gorm:"xingming"`
	Cardid      string  `json:"cardid" gorm:"cardid"`       // 身份证号
	Cardfront   string  `json:"cardfront" gorm:"cardfront"` // 身份证正面
	Cardback    string  `json:"cardback" gorm:"cardback"`   // 身份证反面
	Xitong      int64   `json:"xitong" gorm:"xitong"`
	Fuwuids     string  `json:"fuwuids" gorm:"fuwuids"`
	Yuyuedan    int64   `json:"yuyuedan" gorm:"yuyuedan"` // 1关闭 2接单
}

func Hongniangyyd(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	id := request.Hongniangid
	yuyuedan := request.Yuyuedan
	data_source.Db.Model(&models.Hongniang{}).Where("id = ?", id).Update("yuyuedan", yuyuedan)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Hongniangupdatephone(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	id := request.Hongniangid
	phone := request.Phone
	var hongning models.Hongniang
	data_source.Db.Where("id = ?", id).First(&hongning)
	if hongning.Phone != phone {
		var hongniangs models.Hongniang
		data_source.Db.Where("phone = ?", phone).First(&hongniangs)
		if hongniangs.ID > 0 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "手机号已被使用",
			})
			return
		} else {
			data_source.Db.Model(&models.Hongniang{}).Where("id = ?", id).Update("phone", phone)
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Hongniangsmrz(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	id := request.Hongniangid
	xingming := request.Xingming
	cardid := request.Cardid
	cardfront := request.Cardfront
	cardback := request.Cardback

	hn := models.Hongniang{
		Xingming:  xingming,
		Cardid:    cardid,
		Cardfront: cardfront,
		Cardback:  cardback,
	}
	data_source.Db.Where("id = ?", id).Updates(&hn)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
